package a.happy.town.twobook;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @BelongsProject: twobook
 * @BelongsPackage: a.happy.town.twobook
 * @Author: ByHappy  (Email:15188881427@163.com)
 * @CreateTime: 2023-07-30 22:46
 * @Description:
 * @Version: 1.0
 **/
import static a.happy.town.twobook.UserRegister.*;


public class UserForget {

    // 本地文件路径
    private static File file = new File("user.txt");

    /**
     * 忘记密码流程
     *
     * @param list
     * @author ByHappy
     * @date 12:34 2023/8/6
     **/
    public static void forgetPassword(ArrayList<UserDTO> list) {
        Scanner sc = new Scanner(System.in);

        // 1.判断当前用户是否注册
        System.out.println("请输入用户名:");
        String username = sc.next();
        boolean flag = usernameR(username, null, true);
        if (!flag) {
            System.out.println("当前用户:" + username + "未注册!!!请先注册:");
            return;
        }

        // 2.如果已注册，键盘录入身份证号码
        String personID;
        while (true) {
            System.out.println("请确认您的身份证号码:");
            personID = sc.next();
            boolean flag2 = checkPersonID(personID);
            if (flag2) {
                System.out.println("身份证号 OK! 请进行下一步操作");
                break;
            } else {
                System.out.println("您输入的身份证号码格式错误，请重新输入...");
                continue;
            }
        }
        // 3.键盘录入手机号码
        String phoneNumber;
        while (true) {
            System.out.println("请确认您的手机号码:");
            phoneNumber = sc.next();
            boolean flag3 = checkPhoneNumber(phoneNumber);
            if (flag3) {
                break;
            } else {
                System.out.println("手机号码格式错误，请重新输入...");
                continue;
            }
        }

        // 4.判断拿到的修改密码的信息是否是已有用户的正确信息
        boolean index = findIndeX(username, personID, phoneNumber);
        if (!index) {
            System.out.println("您输入的身份证号码或手机号码输入有误，不能修改密码");
            return;
        }

        //当代码执行到这里，表示所有的数据全部验证成功，直接修改即可
        String password;
        while (true) {
            System.out.println("请输入您的新密码：");
            password = sc.next();
            System.out.println("请再次确认新密码");
            String againPassword = sc.next();
            if (password.equals(againPassword)) {
                System.out.println("两次密码输入一致，OK");
                break;
            } else {
                System.out.println("两次密码输入不一致，请重新输入...");
                continue;
            }
        }

        // 把需要修改的正确密码，写到本地文件里
        try {
            BufferedWriter bfferedWriter = new BufferedWriter(new FileWriter(file, true));
            bfferedWriter.write(username + " " + password + " " + personID + " " + phoneNumber);
            bfferedWriter.newLine();
            bfferedWriter.flush();
            bfferedWriter.close();
            System.out.println("密码修改成功");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * 判断 用户的手机号和身份证号信息是否正确
     *
     * @param username
     * @param personID
     * @param phoneNumber
     * @return boolean
     * @author ByHappy
     * @date 12:44 2023/8/6
     **/
    private static boolean findIndeX(String username, String personID, String phoneNumber) {
        // 立一个flag,假设拿到的是错误的，
        boolean flag = false;

        String line;
        try {
            // 从本地文件读取
            BufferedReader br = new BufferedReader(new FileReader(file));

            try {
                // while 循环 遍历本地用户信息
                while ((line = br.readLine()) != null) {
                    String[] strings = line.split(" ");
                    // 判断需要修改密码用户的用户名，身份证号，手机号是否正确
                    if (username.equals(strings[0]) && personID.equals(strings[2]) && phoneNumber.equals(strings[3])) {
                        // 正确便返回true给flag,
                        flag = true;
                        break;
                    }
                }
            } catch (IOException e) {

                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            //
            e.printStackTrace();
        }
        return flag;
    }


}
